package drds.binlog.binlog_event;

import drds.binlog.common.Authors;
import drds.common.Author;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Author(name = Authors.LI_YANG)
public final class CharSet
{
    static final CharsetInfo[] CHARSET_INFOS = new CharsetInfo[0xff];

    static
    {
        putCharsetInfo(1, "big5", "big5_chinese_ci", "Big5");
        putCharsetInfo(2, "latin2", "latin2_czech_cs", "ISO8859_2");
        putCharsetInfo(3, "dec8", "dec8_swedish_ci", "ISO8859_1");
        putCharsetInfo(4, "cp850", "cp850_general_ci", "Cp850");
        putCharsetInfo(5, "latin1", "latin1_german1_ci", "ISO8859_1");
        putCharsetInfo(6, "hp8", "hp8_english_ci", "ISO8859_1");
        putCharsetInfo(7, "koi8r", "koi8r_general_ci", "KOI8_R");
        putCharsetInfo(8, "latin1", "latin1_swedish_ci", "ISO8859_1");
        putCharsetInfo(9, "latin2", "latin2_general_ci", "ISO8859_2");
        putCharsetInfo(10, "swe7", "swe7_swedish_ci", "ISO8859_1");
        putCharsetInfo(11, "ascii", "ascii_general_ci", "US-ASCII");
        putCharsetInfo(12, "ujis", "ujis_japanese_ci", "EUC_JP");
        putCharsetInfo(13, "sjis", "sjis_japanese_ci", "SJIS");
        putCharsetInfo(14, "cp1251", "cp1251_bulgarian_ci", "Cp1251");
        putCharsetInfo(15, "latin1", "latin1_danish_ci", "ISO8859_1");
        putCharsetInfo(16, "hebrew", "hebrew_general_ci", "ISO8859_8");
        putCharsetInfo(17, "filename", "filename", "ISO8859_1");
        putCharsetInfo(18, "tis620", "tis620_thai_ci", "TIS620");
        putCharsetInfo(19, "euckr", "euckr_korean_ci", "EUC_KR");
        putCharsetInfo(20, "latin7", "latin7_estonian_cs", "ISO8859_7");
        putCharsetInfo(21, "latin2", "latin2_hungarian_ci", "ISO8859_2");
        putCharsetInfo(22, "koi8u", "koi8u_general_ci", "KOI8_U");
        putCharsetInfo(23, "cp1251", "cp1251_ukrainian_ci", "Cp1251");
        putCharsetInfo(24, "gb2312", "gb2312_chinese_ci", "EUC_CN");
        putCharsetInfo(25, "greek", "greek_general_ci", "ISO8859_7");
        putCharsetInfo(26, "cp1250", "cp1250_general_ci", "Cp1250");
        putCharsetInfo(27, "latin2", "latin2_croatian_ci", "ISO8859_2");
        putCharsetInfo(28, "gbk", "gbk_chinese_ci", "GBK");
        putCharsetInfo(29, "cp1257", "cp1257_lithuanian_ci", "Cp1257");
        putCharsetInfo(30, "latin5", "latin5_turkish_ci", "ISO8859_5");
        putCharsetInfo(31, "latin1", "latin1_german2_ci", "ISO8859_1");
        putCharsetInfo(32, "armscii8", "armscii8_general_ci", "ISO8859_1");
        putCharsetInfo(33, "utf8", "utf8_general_ci", "UTF-8");
        putCharsetInfo(34, "cp1250", "cp1250_czech_cs", "Cp1250");
        putCharsetInfo(35, "ucs2", "ucs2_general_ci", "UnicodeBig");
        putCharsetInfo(36, "cp866", "cp866_general_ci", "Cp866");
        putCharsetInfo(37, "keybcs2", "keybcs2_general_ci", "Cp895");
        putCharsetInfo(38, "macce", "macce_general_ci", "MacCentralEurope");
        putCharsetInfo(39, "macroman", "macroman_general_ci", "MacRoman");
        putCharsetInfo(40, "cp852", "cp852_general_ci", "Cp852");
        putCharsetInfo(41, "latin7", "latin7_general_ci", "ISO8859_7");
        putCharsetInfo(42, "latin7", "latin7_general_cs", "ISO8859_7");
        putCharsetInfo(43, "macce", "macce_bin", "MacCentralEurope");
        putCharsetInfo(44, "cp1250", "cp1250_croatian_ci", "Cp1250");
        putCharsetInfo(45, "utf8mb4", "utf8mb4_general_ci", "UTF-8");
        putCharsetInfo(46, "utf8mb4", "utf8mb4_bin", "UTF-8");
        putCharsetInfo(47, "latin1", "latin1_bin", "ISO8859_1");
        putCharsetInfo(48, "latin1", "latin1_general_ci", "ISO8859_1");
        putCharsetInfo(49, "latin1", "latin1_general_cs", "ISO8859_1");
        putCharsetInfo(50, "cp1251", "cp1251_bin", "Cp1251");
        putCharsetInfo(51, "cp1251", "cp1251_general_ci", "Cp1251");
        putCharsetInfo(52, "cp1251", "cp1251_general_cs", "Cp1251");
        putCharsetInfo(53, "macroman", "macroman_bin", "MacRoman");
        putCharsetInfo(54, "utf16", "utf16_general_ci", "UTF-16");
        putCharsetInfo(55, "utf16", "utf16_bin", "UTF-16");
        putCharsetInfo(57, "cp1256", "cp1256_general_ci", "Cp1256");
        putCharsetInfo(58, "cp1257", "cp1257_bin", "Cp1257");
        putCharsetInfo(59, "cp1257", "cp1257_general_ci", "Cp1257");
        putCharsetInfo(60, "utf32", "utf32_general_ci", "UTF-32");
        putCharsetInfo(61, "utf32", "utf32_bin", "UTF-32");
        putCharsetInfo(63, "binary", "binary", "US-ASCII");
        putCharsetInfo(64, "armscii8", "armscii8_bin", "ISO8859_2");
        putCharsetInfo(65, "ascii", "ascii_bin", "US-ASCII");
        putCharsetInfo(66, "cp1250", "cp1250_bin", "Cp1250");
        putCharsetInfo(67, "cp1256", "cp1256_bin", "Cp1256");
        putCharsetInfo(68, "cp866", "cp866_bin", "Cp866");
        putCharsetInfo(69, "dec8", "dec8_bin", "US-ASCII");
        putCharsetInfo(70, "greek", "greek_bin", "ISO8859_7");
        putCharsetInfo(71, "hebrew", "hebrew_bin", "ISO8859_8");
        putCharsetInfo(72, "hp8", "hp8_bin", "US-ASCII");
        putCharsetInfo(73, "keybcs2", "keybcs2_bin", "Cp895");
        putCharsetInfo(74, "koi8r", "koi8r_bin", "KOI8_R");
        putCharsetInfo(75, "koi8u", "koi8u_bin", "KOI8_U");
        putCharsetInfo(77, "latin2", "latin2_bin", "ISO8859_2");
        putCharsetInfo(78, "latin5", "latin5_bin", "ISO8859_5");
        putCharsetInfo(79, "latin7", "latin7_bin", "ISO8859_7");
        putCharsetInfo(80, "cp850", "cp850_bin", "Cp850");
        putCharsetInfo(81, "cp852", "cp852_bin", "Cp852");
        putCharsetInfo(82, "swe7", "swe7_bin", "ISO8859_1");
        putCharsetInfo(83, "utf8", "utf8_bin", "UTF-8");
        putCharsetInfo(84, "big5", "big5_bin", "Big5");
        putCharsetInfo(85, "euckr", "euckr_bin", "EUC_KR");
        putCharsetInfo(86, "gb2312", "gb2312_bin", "EUC_CN");
        putCharsetInfo(87, "gbk", "gbk_bin", "GBK");
        putCharsetInfo(88, "sjis", "sjis_bin", "SJIS");
        putCharsetInfo(89, "tis620", "tis620_bin", "TIS620");
        putCharsetInfo(90, "ucs2", "ucs2_bin", "UnicodeBig");
        putCharsetInfo(91, "ujis", "ujis_bin", "EUC_JP");
        putCharsetInfo(92, "geostd8", "geostd8_general_ci", "US-ASCII");
        putCharsetInfo(93, "geostd8", "geostd8_bin", "US-ASCII");
        putCharsetInfo(94, "latin1", "latin1_spanish_ci", "ISO8859_1");
        putCharsetInfo(95, "cp932", "cp932_japanese_ci", "Shift_JIS");
        putCharsetInfo(96, "cp932", "cp932_bin", "Shift_JIS");
        putCharsetInfo(97, "eucjpms", "eucjpms_japanese_ci", "EUC_JP");
        putCharsetInfo(98, "eucjpms", "eucjpms_bin", "EUC_JP");
        putCharsetInfo(99, "cp1250", "cp1250_polish_ci", "Cp1250");

        putCharsetInfo(101, "utf16", "utf16_unicode_ci", "UTF-16");
        putCharsetInfo(102, "utf16", "utf16_icelandic_ci", "UTF-16");
        putCharsetInfo(103, "utf16", "utf16_latvian_ci", "UTF-16");
        putCharsetInfo(104, "utf16", "utf16_romanian_ci", "UTF-16");
        putCharsetInfo(105, "utf16", "utf16_slovenian_ci", "UTF-16");
        putCharsetInfo(106, "utf16", "utf16_polish_ci", "UTF-16");
        putCharsetInfo(107, "utf16", "utf16_estonian_ci", "UTF-16");
        putCharsetInfo(108, "utf16", "utf16_spanish_ci", "UTF-16");
        putCharsetInfo(109, "utf16", "utf16_swedish_ci", "UTF-16");
        putCharsetInfo(110, "utf16", "utf16_turkish_ci", "UTF-16");
        putCharsetInfo(111, "utf16", "utf16_czech_ci", "UTF-16");
        putCharsetInfo(112, "utf16", "utf16_danish_ci", "UTF-16");
        putCharsetInfo(113, "utf16", "utf16_lithuanian_ci", "UTF-16");
        putCharsetInfo(114, "utf16", "utf16_slovak_ci", "UTF-16");
        putCharsetInfo(115, "utf16", "utf16_spanish2_ci", "UTF-16");
        putCharsetInfo(116, "utf16", "utf16_roman_ci", "UTF-16");
        putCharsetInfo(117, "utf16", "utf16_persian_ci", "UTF-16");
        putCharsetInfo(118, "utf16", "utf16_esperanto_ci", "UTF-16");
        putCharsetInfo(119, "utf16", "utf16_hungarian_ci", "UTF-16");
        putCharsetInfo(120, "utf16", "utf16_sinhala_ci", "UTF-16");

        putCharsetInfo(128, "ucs2", "ucs2_unicode_ci", "UnicodeBig");
        putCharsetInfo(129, "ucs2", "ucs2_icelandic_ci", "UnicodeBig");
        putCharsetInfo(130, "ucs2", "ucs2_latvian_ci", "UnicodeBig");
        putCharsetInfo(131, "ucs2", "ucs2_romanian_ci", "UnicodeBig");
        putCharsetInfo(132, "ucs2", "ucs2_slovenian_ci", "UnicodeBig");
        putCharsetInfo(133, "ucs2", "ucs2_polish_ci", "UnicodeBig");
        putCharsetInfo(134, "ucs2", "ucs2_estonian_ci", "UnicodeBig");
        putCharsetInfo(135, "ucs2", "ucs2_spanish_ci", "UnicodeBig");
        putCharsetInfo(136, "ucs2", "ucs2_swedish_ci", "UnicodeBig");
        putCharsetInfo(137, "ucs2", "ucs2_turkish_ci", "UnicodeBig");
        putCharsetInfo(138, "ucs2", "ucs2_czech_ci", "UnicodeBig");
        putCharsetInfo(139, "ucs2", "ucs2_danish_ci", "UnicodeBig");
        putCharsetInfo(140, "ucs2", "ucs2_lithuanian_ci", "UnicodeBig");
        putCharsetInfo(141, "ucs2", "ucs2_slovak_ci", "UnicodeBig");
        putCharsetInfo(142, "ucs2", "ucs2_spanish2_ci", "UnicodeBig");
        putCharsetInfo(143, "ucs2", "ucs2_roman_ci", "UnicodeBig");
        putCharsetInfo(144, "ucs2", "ucs2_persian_ci", "UnicodeBig");
        putCharsetInfo(145, "ucs2", "ucs2_esperanto_ci", "UnicodeBig");
        putCharsetInfo(146, "ucs2", "ucs2_hungarian_ci", "UnicodeBig");
        putCharsetInfo(147, "ucs2", "ucs2_sinhala_ci", "UnicodeBig");

        putCharsetInfo(160, "utf32", "utf32_unicode_ci", "UTF-32");
        putCharsetInfo(161, "utf32", "utf32_icelandic_ci", "UTF-32");
        putCharsetInfo(162, "utf32", "utf32_latvian_ci", "UTF-32");
        putCharsetInfo(163, "utf32", "utf32_romanian_ci", "UTF-32");
        putCharsetInfo(164, "utf32", "utf32_slovenian_ci", "UTF-32");
        putCharsetInfo(165, "utf32", "utf32_polish_ci", "UTF-32");
        putCharsetInfo(166, "utf32", "utf32_estonian_ci", "UTF-32");
        putCharsetInfo(167, "utf32", "utf32_spanish_ci", "UTF-32");
        putCharsetInfo(168, "utf32", "utf32_swedish_ci", "UTF-32");
        putCharsetInfo(169, "utf32", "utf32_turkish_ci", "UTF-32");
        putCharsetInfo(170, "utf32", "utf32_czech_ci", "UTF-32");
        putCharsetInfo(171, "utf32", "utf32_danish_ci", "UTF-32");
        putCharsetInfo(172, "utf32", "utf32_lithuanian_ci", "UTF-32");
        putCharsetInfo(173, "utf32", "utf32_slovak_ci", "UTF-32");
        putCharsetInfo(174, "utf32", "utf32_spanish2_ci", "UTF-32");
        putCharsetInfo(175, "utf32", "utf32_roman_ci", "UTF-32");
        putCharsetInfo(176, "utf32", "utf32_persian_ci", "UTF-32");
        putCharsetInfo(177, "utf32", "utf32_esperanto_ci", "UTF-32");
        putCharsetInfo(178, "utf32", "utf32_hungarian_ci", "UTF-32");
        putCharsetInfo(179, "utf32", "utf32_sinhala_ci", "UTF-32");

        putCharsetInfo(192, "utf8", "utf8_unicode_ci", "UTF-8");
        putCharsetInfo(193, "utf8", "utf8_icelandic_ci", "UTF-8");
        putCharsetInfo(194, "utf8", "utf8_latvian_ci", "UTF-8");
        putCharsetInfo(195, "utf8", "utf8_romanian_ci", "UTF-8");
        putCharsetInfo(196, "utf8", "utf8_slovenian_ci", "UTF-8");
        putCharsetInfo(197, "utf8", "utf8_polish_ci", "UTF-8");
        putCharsetInfo(198, "utf8", "utf8_estonian_ci", "UTF-8");
        putCharsetInfo(199, "utf8", "utf8_spanish_ci", "UTF-8");
        putCharsetInfo(200, "utf8", "utf8_swedish_ci", "UTF-8");
        putCharsetInfo(201, "utf8", "utf8_turkish_ci", "UTF-8");
        putCharsetInfo(202, "utf8", "utf8_czech_ci", "UTF-8");
        putCharsetInfo(203, "utf8", "utf8_danish_ci", "UTF-8");
        putCharsetInfo(204, "utf8", "utf8_lithuanian_ci", "UTF-8");
        putCharsetInfo(205, "utf8", "utf8_slovak_ci", "UTF-8");
        putCharsetInfo(206, "utf8", "utf8_spanish2_ci", "UTF-8");
        putCharsetInfo(207, "utf8", "utf8_roman_ci", "UTF-8");
        putCharsetInfo(208, "utf8", "utf8_persian_ci", "UTF-8");
        putCharsetInfo(209, "utf8", "utf8_esperanto_ci", "UTF-8");
        putCharsetInfo(210, "utf8", "utf8_hungarian_ci", "UTF-8");
        putCharsetInfo(211, "utf8", "utf8_sinhala_ci", "UTF-8");

        putCharsetInfo(224, "utf8mb4", "utf8mb4_unicode_ci", "UTF-8");
        putCharsetInfo(225, "utf8mb4", "utf8mb4_icelandic_ci", "UTF-8");
        putCharsetInfo(226, "utf8mb4", "utf8mb4_latvian_ci", "UTF-8");
        putCharsetInfo(227, "utf8mb4", "utf8mb4_romanian_ci", "UTF-8");
        putCharsetInfo(228, "utf8mb4", "utf8mb4_slovenian_ci", "UTF-8");
        putCharsetInfo(229, "utf8mb4", "utf8mb4_polish_ci", "UTF-8");
        putCharsetInfo(230, "utf8mb4", "utf8mb4_estonian_ci", "UTF-8");
        putCharsetInfo(231, "utf8mb4", "utf8mb4_spanish_ci", "UTF-8");
        putCharsetInfo(232, "utf8mb4", "utf8mb4_swedish_ci", "UTF-8");
        putCharsetInfo(233, "utf8mb4", "utf8mb4_turkish_ci", "UTF-8");
        putCharsetInfo(234, "utf8mb4", "utf8mb4_czech_ci", "UTF-8");
        putCharsetInfo(235, "utf8mb4", "utf8mb4_danish_ci", "UTF-8");
        putCharsetInfo(236, "utf8mb4", "utf8mb4_lithuanian_ci", "UTF-8");
        putCharsetInfo(237, "utf8mb4", "utf8mb4_slovak_ci", "UTF-8");
        putCharsetInfo(238, "utf8mb4", "utf8mb4_spanish2_ci", "UTF-8");
        putCharsetInfo(239, "utf8mb4", "utf8mb4_roman_ci", "UTF-8");
        putCharsetInfo(240, "utf8mb4", "utf8mb4_persian_ci", "UTF-8");
        putCharsetInfo(241, "utf8mb4", "utf8mb4_esperanto_ci", "UTF-8");
        putCharsetInfo(242, "utf8mb4", "utf8mb4_hungarian_ci", "UTF-8");
        putCharsetInfo(243, "utf8mb4", "utf8mb4_sinhala_ci", "UTF-8");

        putCharsetInfo(254, "utf8", "utf8_general_cs", "UTF-8");
    }

    static CharsetInfo getCharsetInfo(final int id)
    {
        if (id >= 0 && id < CHARSET_INFOS.length)
        {
            return CHARSET_INFOS[id];
        } else
        {
            throw new IllegalArgumentException("Invalid connectionCharset id: " + id);
        }
    }

    static void putCharsetInfo(final int charsetId, String mysqlCharset, String mysqlCollation, String javaCharset)
    {
        CHARSET_INFOS[charsetId] = new CharsetInfo(charsetId, mysqlCharset, mysqlCollation, javaCharset);
    }

    public static String getCharset(final int id)
    {
        CharsetInfo charsetInfo = getCharsetInfo(id);

        if (charsetInfo != null)
        {
            return charsetInfo.mysqlCharset;
        } else
        {
            log.warn("Unexpect mysql connectionCharset: " + id);
            return null;
        }
    }

    public static String getCollation(final int id)
    {
        CharsetInfo charsetInfo = getCharsetInfo(id);

        if (charsetInfo != null)
        {
            return charsetInfo.mysqlCollation;
        } else
        {
            log.warn("Unexpect mysql connectionCharset: " + id);
            return null;
        }
    }

    public static String getJavaCharset(final int id)
    {
        CharsetInfo charsetInfo = getCharsetInfo(id);

        if (charsetInfo != null)
        {
            if (charsetInfo.javaCharset != null)
            {
                return charsetInfo.javaCharset;
            } else
            {
                log.warn("Unknown java connectionCharset for: id = " + id + ", name = " + charsetInfo.mysqlCharset + ", coll = " + charsetInfo.mysqlCollation);
                return null;
            }
        } else
        {
            log.warn("Unexpect mysql connectionCharset: " + id);
            return null;
        }
    }


}
